package cn.wk.org.controller;

import cn.wk.org.domain.Department;
import cn.wk.org.service.IDepartmentService;
import cn.wk.basic.query.DeptQuery;
import cn.wk.basic.utils.AjaxResult;
import cn.wk.basic.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@Controller("/dept")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //@RequestMapping("/queryAll")
    //@ResponseBody
    @PostMapping
    public PageBean<Department> queryAll(@RequestBody DeptQuery deptQuery){  //高级查询
        System.out.println(deptQuery);
        return departmentService.queryAll(deptQuery);
    }

    //    @RequestMapping("/save")
//    @ResponseBody
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){  //添加或者保存

        try{        //判断id
            if(department.getId() != null){
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("save错误");
        }
        return AjaxResult.me();
    }

    //    @RequestMapping("/del")
//    @ResponseBody
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){      //删除
        System.out.println(id);
        try{
            //departmentService.delete(id);
            return AjaxResult.me();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("del错误");
        }
    }

    @PostMapping("/delOne")
    public AjaxResult del(@RequestBody Department department){      //删除
        System.out.println(department.getId());
        //System.out.println(map.get("id"));
        //从对象中取出id的值
        Long id = department.getId();

        try{
            departmentService.delete(id);
            return AjaxResult.me();
        }catch(Exception e){
            e.printStackTrace();
            return new AjaxResult("del错误");
        }
    }

    //    @RequestMapping("/findById")
//    @ResponseBody
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){        //根据id查询
        return departmentService.findById(id);
    }

    //    @RequestMapping("/findAll")
//    @ResponseBody
    @GetMapping
    public List<Department> findAll(){          //查询所有
        return departmentService.findAll();
    }

    //批量删除
    @PostMapping("/batch")
    public AjaxResult delBatch(@RequestBody List<Department> departments){
        System.out.println(departments);
        try{
            departmentService.delBatch(departments);
            return new AjaxResult();
        }catch(Exception e){
            return new AjaxResult("操作失败");
        }
    }

    @PostMapping("/findParent")
    public List<Department> findParent(){
        return departmentService.findParent();
    }

}
